<?php
/**
 * Cookie.php
 *
 * 应用程序 COOKIE 操作封装类.
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	App
 * @package 	App_System
 * @author 		Guya
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/**
 * App COOKIE class
 *
 * @category	App
 * @package 	App_System
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class App_System_Cookie {

	/**
	 * 获取一个COOKIE记录
	 *
	 * @param string $field COOKIE记录KEY
	 * @param string $defaultValue COOKIE中未存在该KEY默认返回值.
	 * @return string
	 */
	public function getCookie($field, $defaultValue = null) {
		if (array_key_exists($field, $_COOKIE)) {
			return $_COOKIE[$field];
		}
		return $defaultValue;
	}

	/**
	 * 获取所有COOKIE内容.
	 *
	 * @return array
	 */
	public function getCookies() {
		return $_COOKIE;
	}

	/**
	 * 设置向客户端发送 COOKIE 信息。
	 *
	 * @param string $field COOKIE记录KEY
	 * @param string $value COOKIE记录KEY值
	 * @param array $params 自定义COOKIE属性数组
	 * @param boolean $classical 发送COOKIE模式。 默认使用PHP自代的setcookie函数。
	 * @return Anjuke_System_Cookie
	 */
	public function setCookie($field, $value = '', $params = array(), $classical = true) {
		if ($field) {
			if ($classical) {
				$this->setCookieByDefault($field, $value, $params);
			} else {
				$this->setCookieByHeader($field, $value, $params);
			}
		}
		return $this;
	}

	/**
	 * 使用 setcookie 函数向客户端发送 COOKIE 信息。
	 *
	 * @param string $field COOKIE记录KEY
	 * @param string $value COOKIE记录KEY值
	 * @param array $params 自定义COOKIE属性数组
	 * @return Anjuke_System_Cookie
	 */
	public function setCookieByDefault($field, $value = '', $params = array()) {
		if ($field) {
			$config = Zend_Registry::get(AJK_APP_CONFIG);
			$cookieConfig = array_merge($config['cookie'], (array)$params);
			setcookie($field, $value, (time() + $cookieConfig['expire']), $cookieConfig['path'], $cookieConfig['domain'], (boolean)$cookieConfig['secure'], (boolean)$cookieConfig['httponly']);
		}
		return $this;
	}


	/**
	 * 使用 send header 模式向客户端发送 COOKIE 信息。
	 *
	 * @param string $field COOKIE记录KEY
	 * @param string $value COOKIE记录KEY值
	 * @param array $params 自定义COOKIE属性数组
	 * @return Anjuke_System_Cookie
	 */
	public function setCookieByHeader($field, $value = '', $params = array()) {
		if ($field) {
			$config = Zend_Registry::get(AJK_APP_CONFIG);
			$cookieConfig = array_merge($config['cookie'], (array)$params);
			$cookieHeader = $field . '=' . urlencode($value) . ';';
			$cookieHeader .= ' path=' . $cookieConfig['path'] . ';';
	        $cookieHeader .= ' domain=' . $cookieConfig['domain'] . ';';
	        if ((int)$cookieConfig['expire']) {
	        	$cookieHeader .= ' expires=' . (time() + $cookieConfig['expire']) . ';';
	        }
	        if ((boolean)$cookieConfig['secure']) {
	        	$cookieHeader .= ' secure;';
	        }
	        if ((boolean)$cookieConfig['httponly']) {
	        	$cookieHeader .= ' httponly';
	        }
			Zend_Controller_Front::getInstance()->getResponse()->setHeader('Set-Cookie', $cookieHeader);
		}
		return $this;
	}
}